package com.cqie.hospitalescort.service.login.UserDetailService;

import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.enums.LoginTerminalEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class CompositeUserDetailsService implements UserDetailsService {

    @Autowired
    private WxUserDetailServiceImpl wxUserDetailsService;

    @Autowired
    private AdminUserDetailServiceImpl adminUserDetailsService;

    @Override
    public AuthUser loadUserByUsername(String username) throws UsernameNotFoundException {
        if (isWxUser(username)) {
            return wxUserDetailsService.loadUserByUsername(username);
        } else {
            return adminUserDetailsService.loadUserByUsername(username);
        }
    }

    private boolean isWxUser(String username) {
        return username.startsWith(LoginTerminalEnum.MINI_PROGRAM.getCode() + "_");
    }
}
